home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / trial / demon / TURNPIKE.1 / CLASSES.ZIP / JAVA / NET / InetAddress.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-14  |  3.3 KB  |  164 lines

  1. package java.net;
  2.  
  3. import java.util.Hashtable;
  4.  
  5. public final class InetAddress {
  6.    private static boolean inCheck;
  7.    String hostName;
  8.    int address;
  9.    int family;
  10.    static Hashtable addressCache;
  11.    static InetAddress unknownAddress;
  12.    static InetAddress anyLocalAddress;
  13.    static InetAddress localHost;
  14.    private static InetAddress loopbackHost;
  15.  
  16.    InetAddress() {
  17.    }
  18.  
  19.    InetAddress(String var1, byte[] var2) {
  20.       this.hostName = new String(var1);
  21.       this.family = getInetFamily();
  22.       this.address = var2[3] & 255;
  23.       this.address |= var2[2] << 8 & '\uff00';
  24.       this.address |= var2[1] << 16 & 16711680;
  25.       this.address |= var2[0] << 24 & -16777216;
  26.    }
  27.  
  28.    public String getHostName() {
  29.       if (this.hostName == null) {
  30.          try {
  31.             this.hostName = getHostByAddr(this.address);
  32.          } catch (UnknownHostException var1) {
  33.             this.hostName = this.getHostAddress();
  34.          }
  35.       }
  36.  
  37.       return this.hostName;
  38.    }
  39.  
  40.    public byte[] getAddress() {
  41.       byte[] var1 = new byte[]{(byte)(this.address >>> 24 & 255), (byte)(this.address >>> 16 & 255), (byte)(this.address >>> 8 & 255), (byte)(this.address & 255)};
  42.       return var1;
  43.    }
  44.  
  45.    public String getHostAddress() {
  46.       return (this.address >>> 24 & 255) + "." + (this.address >>> 16 & 255) + "." + (this.address >>> 8 & 255) + "." + (this.address & 255);
  47.    }
  48.  
  49.    public int hashCode() {
  50.       return this.address;
  51.    }
  52.  
  53.    public boolean equals(Object var1) {
  54.       return var1 != null && var1 instanceof InetAddress && ((InetAddress)var1).address == this.address;
  55.    }
  56.  
  57.    public String toString() {
  58.       return (this.hostName != null ? this.hostName + "/" : "") + this.getHostAddress();
  59.    }
  60.  
  61.    public static synchronized InetAddress getByName(String var0) throws UnknownHostException {
  62.       return var0 != null && var0.length() != 0 ? getAllByName(var0)[0] : loopbackHost;
  63.    }
  64.  
  65.    public static synchronized InetAddress[] getAllByName(String var0) throws UnknownHostException {
  66.       Object var1 = null;
  67.       if (var0 == null) {
  68.          throw new UnknownHostException(var0);
  69.       } else {
  70.          SecurityManager var2 = System.getSecurityManager();
  71.          if (var2 != null && !var2.getInCheck()) {
  72.             var2.checkConnect(var0, -1);
  73.          }
  74.  
  75.          Object var3 = addressCache.get(var0);
  76.          if (var3 == null) {
  77.             try {
  78.                byte[][] var4 = lookupAllHostAddr(var0);
  79.                InetAddress[] var5 = new InetAddress[var4.length];
  80.  
  81.                for(int var6 = 0; var6 < var4.length; ++var6) {
  82.                   byte[] var7 = var4[var6];
  83.                   var5[var6] = new InetAddress(var0, var7);
  84.                   InetAddress[] var8 = new InetAddress[1];
  85.                   String var9 = new String(var5[var6].getHostAddress());
  86.                   var8[0] = new InetAddress(var9, var7);
  87.                   addressCache.put(var9, var8);
  88.                }
  89.  
  90.                var3 = var5;
  91.             } catch (UnknownHostException var11) {
  92.                var3 = unknownAddress;
  93.             }
  94.  
  95.             addressCache.put(var0, var3);
  96.          }
  97.  
  98.          if (var3 == unknownAddress) {
  99.             throw new UnknownHostException(var0);
  100.          } else {
  101.             try {
  102.                var1 = ((InetAddress[])var3).clone();
  103.             } catch (CloneNotSupportedException var10) {
  104.                ((Throwable)var10).printStackTrace();
  105.             }
  106.  
  107.             return (InetAddress[])var1;
  108.          }
  109.       }
  110.    }
  111.  
  112.    public static InetAddress getLocalHost() throws UnknownHostException {
  113.       InetAddress var0 = localHost;
  114.       InetAddress var1 = unknownAddress;
  115.       if (var1 == null || !(var1 instanceof InetAddress) || (var1).address != var0.address && true) {
  116.          SecurityManager var3 = System.getSecurityManager();
  117.  
  118.          try {
  119.             if (var3 != null && !var3.getInCheck()) {
  120.                var3.checkConnect(localHost.getHostName(), -1);
  121.             }
  122.          } catch (SecurityException var2) {
  123.             return loopbackHost;
  124.          }
  125.  
  126.          return localHost;
  127.       } else {
  128.          throw new UnknownHostException();
  129.       }
  130.    }
  131.  
  132.    private static native String getLocalHostName() throws UnknownHostException;
  133.  
  134.    private static native void makeAnyLocalAddress(InetAddress var0);
  135.  
  136.    private static native byte[] lookupHostAddr(String var0) throws UnknownHostException;
  137.  
  138.    private static native byte[][] lookupAllHostAddr(String var0) throws UnknownHostException;
  139.  
  140.    private static native String getHostByAddr(int var0) throws UnknownHostException;
  141.  
  142.    private static native int getInetFamily();
  143.  
  144.    static {
  145.       System.loadLibrary("net");
  146.       addressCache = new Hashtable();
  147.       unknownAddress = new InetAddress();
  148.       anyLocalAddress = new InetAddress();
  149.       makeAnyLocalAddress(anyLocalAddress);
  150.       byte[] var0 = new byte[]{127, 0, 0, 1};
  151.       loopbackHost = new InetAddress("localhost", var0);
  152.  
  153.       try {
  154.          localHost = getByName(getLocalHostName());
  155.       } catch (Exception var3) {
  156.          localHost = unknownAddress;
  157.       }
  158.  
  159.       String var1 = new String("0.0.0.0");
  160.       InetAddress[] var2 = new InetAddress[]{new InetAddress(var1, unknownAddress.getAddress())};
  161.       addressCache.put(var1, var2);
  162.    }
  163. }
  164.